let openid;
layui.config({
    version: '1510164124181'
}).use(['form', 'laydate', 'laypage', 'layer', 'element'], function() {
    form = layui.form;
    layer = layui.layer;
    $ = layui.jquery;
    laypage = layui.laypage; //分页
    element = layui.element; //元素操作
    openid = GetQueryString("openid");
    if (openid == null || openid == undefined || openid == "") {
        openid = 0;
    } else {
        UserInfoList();
    }

});

function UserInfoList() {
    let index = top.layer.msg('加载中，请稍候', { icon: 16, time: false, shade: 0.8 });
    $.ajax({
        url: httpurl + "/findSpUserListOpenId",
        cache: false,
        data: {
            "openid": openid
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            top.layer.closeAll();
            // console.log(JSON.stringify(data));

            if (data.rtnCode == 200) {
                let dataList = data.data.sp_user;
                $("#headimg").attr("src", dataList.headimg);
                $("#user_name").val(dataList.username);
                $("#nickname").val(dataList.nickname);
                $("#tel").val(dataList.phone);
                $("#openid_info").val(dataList.openid);
                $("#my_money").val(dataList.goldnum);
                $("#member").val(data.data.memberSetup.name);
                $("#intime").val(dataList.intime);
                $("#sex input[value=" + dataList.sex + "]").attr("checked", true);
                $("#isfollow option[value=" + dataList.isfollow + "]").attr("selected", true);
            }
        },
        error: function(data) {
            top.layer.closeAll();
        }
    });
}

function SelectUserOrderById() {
    $.ajax({
        url: httpurl + "/SelectUserOrderById",
        cache: false,
        data: {
            "UserOrderId": UserOrderId,
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        // dataType: "json",
        success: function(data) {

        }
    });
}

function getqueryMemberTaskRecordList(mode) {
    $.ajax({
        url: httpurl + "/queryMemberTaskRecordList",
        cache: false,
        data: {
            "pageNo": pageNo,
            "pageSize": pageRecords,
            "openid": openid,
            "mode":mode||''
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            // console.log(JSON.stringify(data));
            let dataHtml = "";
            if (data.rtnCode == 200) {
                let initdata = data.data.list;
                totalRecords = data.data.total;
                initdata.forEach(v=>{
                    if(v.type==1){
                        dataHtml+=`<tr>
                            <td>${v.note}</td>
                            <td style="color:red">增加</td>
                            <td>${v.addNum}</td>
                            <td>${v.createTime}</td>
                        </tr>`
                    }else{
                        dataHtml+=`<tr>
                            <td>${v.note}</td>
                            <td style="color:#4bcf99">减少</td>
                            <td>${v.addNum}</td>
                            <td>${v.createTime}</td>
                        </tr>`
                    }
                    
                })
            } else {
                totalRecords = 0;
                dataHtml += '<tr>' +
                    '<td colspan="12" style="text-align:center;">暂无数据</td>' +
                    '</tr>';
            }
            $(".recordrpage tbody").html(dataHtml);
            layer.closeAll();
            jumpobj = "recordrpage";
            page();


        },
        error: function(data) {
            layer.msg("出现错误,请稍后再试!");
        }
    });
}
function CouponListByOpenid() {
    $.ajax({
        url: httpurl + "/CouponListByOpenid",
        cache: false,
        data: {
            "page": pageNo,
            "pagesize": pageRecords,
            "openid": openid
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            //console.log("CouponListByOpenid===="+JSON.stringify(data));
            let dataHtml = "";
            if (data.rtnCode == 200) {
                let initdata = data.data;
                totalRecords = initdata[initdata.length - 1].total;
                for (let i = 0; i < initdata.length; i++) {
                    if(initdata[i].couponEntity){
                        dataHtml += '<tr>' +
                        '<td>' + initdata[i].couponEntity.name + '</td>' +
                        '<td>' + initdata[i].couponEntity.val + '</td>' +
                        '<td>' + initdata[i].couponEntity.fullmoney + '</td>' +
                        '<td>' + (initdata[i].istype == 0 ? "待使用" : (initdata[i].istype == 1 ? "已使用" : (initdata[i].istype == 2 ? "已过期" : ""))) + '</td>' +
                        '</tr>';
                    }
                    
                }
            } else {
                totalRecords = 0;
                dataHtml += '<tr>' +
                    '<td colspan="12" style="text-align:center;">暂无数据</td>' +
                    '</tr>';
            }

            $(".couponrpage tbody").html(dataHtml);
            layer.closeAll();
            jumpobj = "couponrpage";
            page();
        }
    });
}
//分页
function page() {
    laypage.render({
        elem: jumpobj //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: totalRecords //数据总数，从服务端得到
            ,
        curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
            ,
        hash: 'fenye' //自定义hash值
            ,
        layout: ['count', 'prev', 'page', 'next', 'skip'],
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr;
                pageRecords = obj.limit; // 当前页面记录数
                if (jumpobj == 'couponrpage') {
                    CouponListByOpenid();
                }else if (jumpobj == 'orderpage') {
                    OrderListByOpenid();
                }else if(jumpobj == 'recordrpage'){
                    getqueryMemberTaskRecordList()
                }
            }
        }
    });
}

function OrderListByOpenid() {
    $.ajax({
        url: httpurl + "/OrderListByOpenid",
        cache: false,
        data: {
            "page": pageNo,
            "pagesize": pageRecords,
            "openid": openid
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            // console.log("===="+JSON.stringify(data));
            let dataHtml = "";
            if (data.rtnCode == 200) {
                let initdata = data.data;
                totalRecords = initdata[initdata.length - 1].total;
                for (let i = 0; i < initdata.length; i++) {
                    let ClassInfo = "text-primary";
                    if (initdata[i].type == 1) {
                        ClassInfo = "text-success";
                    } else if (initdata[i].type == 2) {
                        ClassInfo = "text-warning";
                    }
                    dataHtml += '<tr class="' + ClassInfo + '">' +
                        '<td>' + initdata[i].id + '</td>' +
                        '<td>' + initdata[i].name + '</td>' +
                        '<td>' + initdata[i].phone + '</td>' +
                        '<td>' + initdata[i].intime + '</td>' +
                        '<td>' + initdata[i].ordermoney + '</td>' +
                        '<td>' + (initdata[i].state == 0 ? "待付款" : (initdata[i].state == 1 ? "待发货" : (initdata[i].state == 2 ? "待收货" : (initdata[i].state == 3 ? "已收货" : (initdata[i].state == 4 ? "售后" : (initdata[i].state == 5 ? "已取消" : (initdata[i].state == 6 ? "备货" : "未知"))))))) + '</td>' +
                        '<td>' + (initdata[i].type == 0 ? "普通订单" : (initdata[i].type == 1 ? "秒杀订单" : (initdata[i].type == 2 ? "拼单订单" : ((initdata[i].type == 3 ? "免费领取" : ""))))) + '</td>' +
                        '<td>' +
                        '<a href="../order/DetaileOrder.html?OrderId=' + initdata[i].id + '">编辑</a>&nbsp;&nbsp;&nbsp;' +
                        '<a href="#" onclick="UpdateOrderState(\'' + initdata[i].id + '\');">取消</a>&nbsp;&nbsp;&nbsp;' +
                        '<a href="#" data-id="' + initdata[i].id + '"  data-info="deleteOrderById" onclick="DeleteInfo(this);">删除</a>' +
                        '</td>' +
                        '</tr>';
                }
            } else {
                totalRecords = 0;
                dataHtml += '<tr>' +
                    '<td colspan="12" style="text-align:center;">暂无数据</td>' +
                    '</tr>';
            }
            $(".orderpage tbody").html(dataHtml);
            layer.closeAll();
            jumpobj = "orderpage";
            page();

        }
    });
}

function SubmitInfo() {
    let headimg = $("#headimg").attr("name");
    let user_name = $("#user_name").val();
    let nickname = $("#user_name").val();
    let phone = $("#tel").val();
    // let openid_info = $("#openid_info").val();
    let sex = $("#sex input:checked").val();
    let isfollow = $("#isfollow option:selected").val();
    let my_money = $("#my_money").val();
    let card_end_time = $("#card_end_time").val();
    if (user_name == "") {
        top.layer.alert("用户名不能为空哟!");
        return;
    }
    if (phone == "") {
        top.layer.alert("手机号不能为空哟!");
        return;
    }
    let httpAjax = "UpdateUserByOpenId";
    if (openid == 0) {
        httpAjax = "InsertSpUserPc";
    }
    $.ajax({
        type: "POST",
        url: httpurl + "/" + httpAjax,
        cache: false,
        data: JSON.stringify({
            "openid": openid,
            "headimg": headimg,
            "username": user_name,
            "phone": phone,
            "goldnum": my_money,
            "sex": sex,
            "isfollow": isfollow,
            "nickname": nickname,
            "my_money": my_money,
            "card_end_time": card_end_time
        }),
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        success: function(data) {
            console.log(JSON.stringify(data));
            if (data.rtnCode == 200) {
                top.layer.alert("提交成功!", function(index) {
                    top.location.href = "../UserList.html";
                    layer.close(index);
                });
            }
        },
        error: function(data) {
            layer.msg("出现错误,请稍后再试!");
        }
    });
}
//
// function SelectUserOrderList() {
//     var index = top.layer.msg('加载中，请稍候', {icon: 16, time: false, shade: 0.8});
//     $.ajax({
//         type: "POST",
//         url: httpurl + "/SelectUserOrderList",
//         cache: false,
//         data: {
//             "openid": openid
//         },
//         headers: {
//             "Access-Control-Allow-Origin": "*",
//             "Access-Control-Allow-Methods": "*",
//             "Access-Control-Allow-Headers": "Authorization",
//             "Authorization": GetTokenInfo()
//         },
//         contentType : 'application/json',
//         success: function (data) {
//             top.layer.closeAll();
//             console.log(JSON.stringify(data));
//         }
//     });
// }